package com.twt.controller;

import com.twt.Dao.MessageDao;
import com.twt.Dao.PickLakeDao;
import com.twt.entity.Message;
import com.twt.entity.Result;
import com.twt.entity.picked;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/Message")
public class MessageController {
    @Autowired
    private PickLakeDao pickLakeDao;
    @Autowired
    private MessageDao messageDao;

    /**
     * 发送消息
     *
     * @param bottleId,username,Message 参数封装
     * @return Result
     */
    //http://localhost:8080/Message/send?username=wanghao&bottleId=20&Message=hh
    @PostMapping("/send")
    public Result SendMessage(@RequestParam("bottleId") int bottleId,
                              @RequestParam("username") String username,
                              @RequestParam("Message") String Message) {
        Result result = new Result();
        result.setMsg("消息发送失败");
        result.setSuccess(false);
        result.setDetail(null);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//设置日期格式
        String Time = df.format(new Date());// new Date()为获取当前系统时间
        String nickname = pickLakeDao.findNicknameByUsername(username);
        List<com.twt.entity.Message> list = messageDao.getName(bottleId);
        com.twt.entity.Message message = list.get(0);
        messageDao.insertMessage(bottleId, username, Message, Time);
        if (username != message.getPickUserName()) {
            messageDao.setNewMessageInPickTrue(bottleId, username);
        } else {
            messageDao.setNewMessageInThrowTrue(bottleId, username);
        }
        result.setSuccess(true);
        result.setMsg("发送消息成功");
        return result;
    }

    /**
     * 查询消息
     *
     * @param bottleId 参数封装
     * @return Result
     */
    //http://localhost:8080/Message/view?bottleId=20&username=xuyaohui
    @GetMapping("/view")
    public Result findMessage(@RequestParam("bottleId") int bottleId,
                              @RequestParam("username") String username) {
        Result result = new Result();
        result.setMsg("查询失败");
        result.setSuccess(false);
        result.setDetail(null);
        List<Message> message = messageDao.getName(bottleId);
        com.twt.entity.Message message1 = message.get(0);
        String name = null;
        if (Objects.equals(username, message1.getPickUserName())) {
            name = message1.getThrowUserName();
            messageDao.setNewMessageInPickFalse(bottleId, name);
        } else {
            name = message1.getPickUserName();
            messageDao.setNewMessageInThrowFalse(bottleId, name);
        }

        Collections.sort(message);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//设置日期格式
        for (int i = message.size() - 1; i >= 1; i--) {
            try {
                Date date1 = df.parse(message.get(i).getTime());
                Date date2 = df.parse(message.get(i - 1).getTime());
                if ((date1.getTime() - date2.getTime()) / 1000 / 60 < 10)
                    message.get(i).setTime(null);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        result.setMsg("查询成功");
        result.setDetail(message);
        result.setSuccess(true);
        return result;
    }

    /**
     * 置顶聊天
     *
     * @param bottleId 参数封装
     * @return Result
     */
    //http://localhost:8080/Message/setTop?bottleId=21&username=xuyaohui&top=1
    @PostMapping("/setTop")
    public Result setTopBottle(@RequestParam("bottleId") int bottleId,
                               @RequestParam("username") String username, @RequestParam("top") int top) {
        Result result = new Result();
        result.setResult("设置失败", false);
        if (top > 1 || top < 0) {
            result.setResult("设置失败,top只能是0或1", false);
            return result;
        }
        messageDao.setTopInThrowed(bottleId, username, top);
        messageDao.setTopInPicked(bottleId, username, top);
        result.setResult("设置成功", true);
        return result;
    }

    /**
     * 销毁瓶子
     *
     * @param bottleId 参数封装
     * @return Result
     */
    //http://localhost:8080/Message/deleteBottle?bottleId=21&username=xuyaohui
    @PostMapping("/deleteBottle")
    public Result deleteBottle(@RequestParam("bottleId") int bottleId,
                               @RequestParam("username") String username) {
        Result result = new Result();
        result.setResult("删除失败", false);
        List<Message> message = messageDao.getName(bottleId);
        com.twt.entity.Message message1 = message.get(0);
        String name = null;
        if (Objects.equals(username, message1.getPickUserName())) {
            messageDao.setUserNonexistentInThrowedByBottleId(bottleId, username);
            name = message1.getThrowUserName();
            messageDao.setUserNonexistentInPickedByBottleId(bottleId, name);
        } else {
            name = message1.getPickUserName();
            messageDao.setUserNonexistentInPickedByBottleId(bottleId, username);
            messageDao.setUserNonexistentInThrowedByBottleId(bottleId, name);
        }
        result.setResult("删除成功", true);
        return result;

    }
}
